﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Benefit.Service.ServerData
{
    /// <summary>
    /// 真实账户成交明细
    /// </summary>
    public class ServiceT_RealOrderTradeDetail : Benefit.Interface.ServerData.IT_RealOrderTradeDetail
    {
        Benefit.DB.DBManager db = null;
        public ServiceT_RealOrderTradeDetail(Benefit.DB.DBManager db)
        {
            this.db = db;
        }
        public void Init(string pdate)
        {
            Interface.ServerData.IT_RealOrderTradeDetail h = new ServerData.ServiceT_RealOrderTradeDetail(db);
            //  Models.ServerData.T_RealOrderTradeDetail h = new Data.ServerData.T_RealOrderTradeDetail();
            List<Models.ServerData.T_RealOrderTradeDetail> tsm = h.GetInfo(pdate);
            foreach (Models.ServerData.T_RealOrderTradeDetail ha in tsm)
            {
                db.RealOrderTradeDetail.Add(ha);
            }
            db.SaveChanges();
        }

        /// <summary>
        /// 查询结算后的资金情况
        /// </summary>
        /// <param name="accountid"></param>
        /// <param name="pdate"></param>
        /// <returns></returns>
        public System.Collections.Generic.List<Models.ServerData.T_RealOrderTradeDetail> GetInfo(string pdate)
        {
            List<Models.ServerData.T_RealOrderTradeDetail> ios = new List<Models.ServerData.T_RealOrderTradeDetail>();
            Interface.ServerData.IT_Settle_History tsh = new ServerData.ServiceT_Settle_History(db);
            // T_Settle_History tsh = new T_Settle_History();
            int settlementid = tsh.GetDayMaxSettlementInfoId(pdate);
            if (settlementid != -1)
            {
                ios = GetRealOrderTradeDetailInfo(settlementid, pdate);
            }
            return ios;
        }

        /// <summary>
        /// 查询某天最大结算编号的资金记录
        /// </summary>
        /// <param name="settlementid">结算编号</param>
        /// <param name="pdate">日期</param>
        /// <returns></returns>
        public System.Collections.Generic.List<Models.ServerData.T_RealOrderTradeDetail> GetRealOrderTradeDetailInfo(int settlementid, string pdate)
        {
            DavidDB.ADO.DBManager dbmanager = new DavidDB.ADO.DBManager();
            System.Collections.Generic.List<Models.ServerData.T_RealOrderTradeDetail> tsm = new System.Collections.Generic.List<Models.ServerData.T_RealOrderTradeDetail>();
            string sql = "select * from T_RealOrderTradeDetail where SettlementId='" + settlementid + "' and tradingday='" + pdate + "'";
            dynamic sdr = dbmanager.ExecuteReader(sql);
            while (sdr.Read())
            {
                Models.ServerData.T_RealOrderTradeDetail s = new Models.ServerData.T_RealOrderTradeDetail();
                s.BrokerId = sdr["BrokerId"].ToString();
                s.BrokerOrderSeq = sdr["BrokerOrderSeq"].ToString();
                s.BusinessUnit = sdr["BusinessUnit"].ToString();
                s.ClearingPartId = sdr["ClearingPartId"].ToString();
                s.ClientId = sdr["ClientId"].ToString();
                s.Direction = Convert.ToInt32(sdr["Direction"]);
                s.DradeTime = sdr["DradeTime"].ToString();
                s.ExchangeId = sdr["ExchangeId"].ToString();
                s.ExchangeInstId = sdr["ExchangeInstId"].ToString();
                s.HedgeFlag = Convert.ToInt32(sdr["HedgeFlag"]);
                s.Id = Convert.ToInt32(sdr["Id"]);
                s.InstrumentId = sdr["InstrumentId"].ToString();
                s.InvestOrid = sdr["InvestOrid"].ToString();
                s.OffsetFlag = Convert.ToInt32(sdr["OffsetFlag"]);
                s.OrderLocalId = sdr["OrderLocalId"].ToString();
                s.OrderRef = Convert.ToInt32(sdr["OrderRef"]);
                s.OrderSysId = sdr["OrderSysId"].ToString();
                s.ParticiPantId = sdr["ParticiPantId"].ToString();
                s.Price = Convert.ToDouble(sdr["Price"]);
                s.PriceSource = sdr["PriceSource"].ToString();
                s.SequenceNo = sdr["SequenceNo"].ToString();
                s.SettleMentId = sdr["SettleMentId"].ToString();
                s.TradeDate = sdr["TradeDate"].ToString();
                s.TradeId = sdr["TradeId"].ToString();
                s.TradeOjbId = Convert.ToInt32(sdr["TradeOjbId"]);
                s.TraderId = sdr["TraderId"].ToString();
                s.TradeType = sdr["TradeType"].ToString();
                s.TradingDay = sdr["TradingDay"].ToString();
                s.TradingRole = sdr["TradingRole"].ToString();
                s.UserId = Convert.ToInt32(sdr["UserId"]);
                s.Volume = Convert.ToInt32(sdr["Volume"]);
                tsm.Add(s);
            }
            sdr.Close();
            dbmanager.Commit();
            dbmanager.Dispose();
            return tsm;
        }
    }
}
